package com.example.fourinarow;

import java.util.List;
import android.app.Activity;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class DbHandler extends Activity implements OnClickListener {

	TextView stats;
	Button delete;
	ImageView header;
	public static MySQLiteHelper db;
	private int click;
	private String statName = "";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.statistic_layout);
		setupDb();
		setupLayout();
	}

	// Initiates the database
	private void setupDb() {
		db = new MySQLiteHelper(this);
		List<Statistic> list = db.getAllStatistics();

		if (list.isEmpty()) {
			db.addStatistic(new Statistic("Segrar ", 0));
			db.addStatistic(new Statistic("Nederlag ", 0));
			db.addStatistic(new Statistic("Oavgjorda ", 0));
		}

		for (Statistic s : list) {
			statName += s.getName() + ": " + s.getCount() + "\n";
		}
	}
	
	// Initiates the layout for statistics
	private void setupLayout() {
		stats = (TextView) findViewById(R.id.stats);
		stats.setText(statName);
		stats.setTypeface(Typeface.createFromAsset(this.getAssets(),
				"fonts/stereofidelic.ttf"));
		stats.setTextSize(TypedValue.COMPLEX_UNIT_SP, 40);
		stats.setTextColor(Color.parseColor("#E007CE"));

		header = (ImageView) findViewById(R.id.stathead);
		header.setOnClickListener(this);
		delete = (Button) findViewById(R.id.delete);
		delete.setOnClickListener(this);
		delete.setVisibility(View.GONE);
	}

	@Override
	// Checks if you hit the image (Statistik) 7 times and the hidden button pops up which clears the database if u press it
	public void onClick(View v) {

		if (v.getId() == R.id.delete) {
			List<Statistic> list = db.getAllStatistics();
			for (Statistic s : list) {

				db.deleteStatistics(s);
			}
		} else if (v.getId() == R.id.stathead) {
			click++;
			if (click == 7) {
				delete.setVisibility(View.VISIBLE);
				click = 0;
			}
		}
	}

}
